package com.example.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nft.dao.Tutu2Mapper;
import com.example.nft.entity.Tutu2;
import com.example.nft.service.Tutu2Service;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository("Tutu2Service")
public class Tutu2ServiceImpl implements Tutu2Service {
    @Resource
    private Tutu2Mapper tutu2Mapper;


    @Override
    public List<Tutu2> selectAll(String fenqu, String name) {
        QueryWrapper<Tutu2> queryWrapper = new QueryWrapper<> ();
        if (fenqu!=null&&!("".equals(fenqu))) {
            queryWrapper.like ("fenqu", fenqu);
        }
        if (name!=null&&!("".equals(name))) {
            queryWrapper.like ("name", name);
        }
        if (name==null&&fenqu==null){
            return tutu2Mapper.selectList(null);
        }
        List<Tutu2> tutus = tutu2Mapper.selectList (queryWrapper);
        return tutus;
    }

    @Override
    public List<Tutu2> selectAll() {
        return tutu2Mapper.selectList(null);
    }


    @Override
    public String insert() {
        String video1 = null;
        List<String> videohtmls;
        List<String> htmls = getHtmls ();
        for (String s : htmls) {
            if (s.contains (":void(0)")){
                continue;
            }
            videohtmls = getVideoHtml (s);
            for (int i = 1; i < videohtmls.size (); i++) {
                if (s.contains (":void(0)")){
                    continue;
                }
                getVideo(videohtmls.get (i),videohtmls.get (0));
            }
        }
        return video1;
    }










    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }


    public  String getVideo(String html,String fenqu){
        try {
            String html1 = getHtml(html);
            Document document = Jsoup.parse(html1);
            Elements wrap = document.select (".film_bar.clearfix");
            String title = document.getElementsByTag("title").get(0).text();
            Element elemente = wrap.get (wrap.size () - 1);
            Elements script = elemente.select ("script");
            Element element = script.get (script.size () - 1);
            String replace = element.data ().replace (
                    "\tvar down_url = 'https://d.9xxav.com", "").substring (0,30).replace ("'","").replace (";","").trim ();

            Tutu2 tutu2 = new Tutu2 ();
            tutu2.setName (title);
            tutu2.setAddress ("https://11xiaxnduuq.com"+replace);
            tutu2.setFenqu (fenqu);
            tutu2.setDatetime (new Date ());
            tutu2Mapper.insert (tutu2);
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return "执行成功";
    }

    public static Integer getindex(String html){
        String index_ = null;
        try {
            String html1 = getHtml(html);
            String s ="https://www.99v89.xyz";
            Document document = Jsoup.parse(html1);
            Elements a1 = document.select(".next.pagegbk").select("a");
            if (a1.size ()>1) {
                String index = s + a1.get (a1.size () - 1).attr ("href");
                index_ = index.replace (html, "").replace ("index-", "").replace (".html", "").replace ("/", "").trim ();
            }else {
                index_= "0";
            }
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        return Integer.valueOf(index_);
    }

    public static List<String> getVideoHtml(String html){
        List<String> list = null;
        try {
            String url = "https://www.99v89.xyz/Html/"+html;
            Integer getindex = getindex(url);
            list = new ArrayList<> ();
            list.add (html);
            for (int i = 1; i<=getindex; i++){
                StringBuilder htmlBuilder = new StringBuilder(url);
                htmlBuilder.append("/index-").append(i).append(".html");
                String s = htmlBuilder.toString();
                s = getHtml(s);
                Document document = Jsoup.parse(s);
                Elements wrap = document.select (".box.movie2_list.gfzgfdbbf");
                for (Element element : wrap) {
                    Elements a = element.getElementsByTag ("li");
                    for (Element element1 : a) {
                        String attr = element1.select ("a").attr ("href");
                        list.add ("https://www.99v89.xyz"+attr);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return list;
    }

    public static List<String> getHtmls() {

        List<String> list = null;
        try {
            List<Integer> number = new ArrayList ();
            list = new ArrayList ();
            for (int i = 61; i < 63; i++) {
                number.add (i);
            }
            number.add (84);
            for (int i = 87; i < 92; i++) {
                number.add (i);
            }
            number.add (93);
            number.add (94);
            number.add (100);
            number.add (101);
            for (int i = 109; i <115 ; i++) {
                number.add (i);
            }

            for (Integer integer : number) {
                String url = "https://www.99v89.xyz/Html/"+ integer;
                list.add (integer.toString ());
                list.add (url);
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return list;
    }
}
